package cn.edu.zjicm.wordsnet_d.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;

public class ExperienceNumRegularView extends View {
	private int rowNum = 6;
	private int colNum = 2;
	private float startX = 0;
	private float startY = 0;
	private float firstCellWidth;
	private float otherCellWidth;
	private float allCellHeight;
	private String[] firstRow = { "熟悉度变化", "产生经验值" };
	private String[] firstCol = { "未学-->太简单", "未学-->生词-熟悉度1", "生词-熟悉度1-->生词-熟悉度2", "生词-熟悉度2-->生词-熟悉度3", "生词-熟悉度3-->熟词-熟悉度4" };
	private String experienceNum = "10";

	public ExperienceNumRegularView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		drawCells(canvas);
		drawFirstRowBackground(canvas);
		drawOtherCells(canvas);
		drawContents(canvas);
	}

	private void drawFirstRowBackground(Canvas canvas) {

		// 向内绘制圆角型矩形的
		Paint mPaint = new Paint();
		mPaint.setStyle(Style.FILL);
		mPaint.setAntiAlias(true);
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(56, 59, 65));
		else
			mPaint.setColor(Color.WHITE);
		RectF r1 = new RectF(startX + 2, startY + 2, startX + getWidth() - 2, startY + getHeight() - 2);
		canvas.drawRoundRect(r1, 10, 8, mPaint);

		// 绘制第一行的圆角矩形
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(67, 72, 85));
		else
			mPaint.setColor(Color.rgb(244, 244, 244));
		RectF r2 = new RectF(startX + 2, startY + 2, startX + getWidth() - 2, startY + allCellHeight + 20);
		canvas.drawRoundRect(r2, 10, 8, mPaint);

		// 将绘制第一行圆角矩形产生的多余部分去掉
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(56, 59, 65));
		else
			mPaint.setColor(Color.WHITE);
		canvas.drawRect(startX + 2, startY + allCellHeight, getWidth() - 2, startY + allCellHeight * 2, mPaint);
	}

	private void drawCells(Canvas canvas) {
		firstCellWidth = getWidth() * 2 / 3;
		otherCellWidth = getWidth() * 1 / 3;
		allCellHeight = getHeight() / 6;

		// 绘制圆角型矩形的
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);

		if (NightModeUtil.isNightModeNow()) {
			mPaint.setColor(Color.rgb(31, 32, 35));
		} else
			mPaint.setColor(Color.rgb(192, 192, 192));
		mPaint.setStyle(Style.FILL);
		RectF r = new RectF(startX, startY, getWidth(), getHeight());
		canvas.drawRoundRect(r, 10, 8, mPaint);
	}

	// 设置画笔
	private Paint setPaint() {
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(0);
		mPaint.setStyle(Style.STROKE);
		mPaint.setColor(Color.rgb(192, 192, 192));
		return mPaint;
	}

	private void drawOtherCells(Canvas canvas) {
		Paint mPaint = setPaint();
		if (NightModeUtil.isNightModeNow()) {
			mPaint.setColor(Color.rgb(31, 32, 35));
		}
		// 绘制横线
		for (int i = 1; i < rowNum; i++) {
			float start_x = startX;
			float start_y = startY + allCellHeight * i;
			canvas.drawLine(start_x, start_y, getWidth(), start_y, mPaint);
		}

		// 绘制竖线
		for (int i = 0; i < colNum - 1; i++) {
			float start_x = startX + firstCellWidth + otherCellWidth * i;
			float stop_x = start_x;
			float start_y = startY;
			float stop_y = startY + allCellHeight * rowNum;
			canvas.drawLine(start_x, start_y, stop_x, stop_y, mPaint);
		}
	}

	private void drawContents(Canvas canvas) {
		Paint mPaint = setPaint();
		mPaint.setTextSize(otherCellWidth / 8);
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(166, 175, 188));
		else
			mPaint.setColor(Color.rgb(51, 51, 51));

		// drawFirstRow
		int textHeight;
		for (int i = 0; i < colNum; i++) {
			float start_x;
			if (i == 0) {
//				start_x = startX + (firstCellWidth - getTextWidth(mPaint, firstRow[i])) / 2;   //居中对齐
				start_x = startX + 25;
			} else {
				start_x = startX + firstCellWidth + (otherCellWidth - getTextWidth(mPaint, firstRow[i])) / 2;
			}
			textHeight = getTextHeight(mPaint, firstRow[i]);
			float start_y = startY + (allCellHeight - textHeight) / 2 + textHeight;
			canvas.drawText(firstRow[i], start_x, start_y, mPaint);
		}

		// drawFirstCol
		for (int i = 0; i < rowNum - 1; i++) {
			textHeight = getTextHeight(mPaint, firstCol[i]);
//			float start_x = startX + (firstCellWidth - getTextWidth(mPaint, firstCol[i])) / 2;  //居中对齐
			float start_x = startX + 25;
			float start_y = startY + allCellHeight * (i + 1) + (allCellHeight - textHeight) / 2 + textHeight;
			mPaint.setTextSize(otherCellWidth / 8);
			canvas.drawText(firstCol[i], start_x, start_y, mPaint);
		}
		
		textHeight = getTextHeight(mPaint, experienceNum);
		for (int i = 0; i < rowNum - 1; i++) {
			float start_x = startX + firstCellWidth + (otherCellWidth - getTextWidth(mPaint, experienceNum)) / 2;
			float start_y = startY + allCellHeight * (i + 1) + (allCellHeight - textHeight) / 2 + textHeight;
			mPaint.setTextSize(otherCellWidth / 8);
			canvas.drawText(experienceNum, start_x, start_y, mPaint);
		}
	}

	private int getTextWidth(Paint paint, String text) {
		Rect textBounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), textBounds);
		int textWidth = textBounds.right - textBounds.left;
		return textWidth;
	}

	private int getTextHeight(Paint paint, String text) {
		Rect textBounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), textBounds);
		int textHeight = textBounds.bottom - textBounds.top;
		return textHeight;
	}

	public Paint getPaintColor(Paint mPaint, int newAddWords) {
		if (newAddWords == 0) {
			if (NightModeUtil.isNightModeNow())
				mPaint.setColor(Color.rgb(166, 175, 188));
			else
				mPaint.setColor(Color.rgb(51, 51, 51));
		} else if (newAddWords > 0)
			mPaint.setColor(Color.rgb(218, 107, 84));
		else
			mPaint.setColor(Color.rgb(23, 160, 96));
		return mPaint;
	}

}
